package main

import (
	"context"
	"errors"
	"fmt"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"outsourced-projects/pkg/communal"
	"outsourced-projects/pkg/utils"
	"outsourced-projects/service"
	admin2 "outsourced-projects/service/admin"
	"strings"
)

//接口鉴定权限
func AdminJwtAuthentication(c *gin.Context) {
	if c.Request.Header.Get("Authorization") == "" {
		service.ReturnJson(c, 401, "jwt不能为空", gin.H{})
		c.Abort()
		return
	}

	jwt := strings.Split(c.Request.Header.Get("Authorization"), " ")
	if len(jwt) <= 1 {
		service.ReturnJson(c, 401, "jwt不能为空", gin.H{})
		c.Abort()
		return
	}

	jwtData, err := utils.ParseToken(jwt[1])
	if err != nil && err.Error() == "令牌错误" {
		service.ReturnJson(c, 401, err.Error(), gin.H{})
		c.Abort()
		return
	}

	if err != nil {
		service.ReturnJson(c, 401, err.Error(), gin.H{})
		c.Abort()
		return
	}

	if jwtData == nil { //非空
		service.ReturnJson(c, 401, errors.New("jwt不存在").Error(), gin.H{})
		c.Abort()
		return
	}

	redisKey := fmt.Sprintf("outsourcedProjects:UserAccount:%d", cast.ToInt(jwtData.Id))
	redisVal := communal.Redis().Get(context.Background(), redisKey).Val()
	if redisVal == "" {
		service.ReturnJson(c, 401, errors.New("请先登录").Error(), gin.H{})
		c.Abort()
		return
	}
	if redisVal != jwt[1] {
		service.ReturnJson(c, 401, errors.New("请先登录").Error(), gin.H{})
		c.Abort()
		return
	}

	c.Set("userid", jwtData.Id)
	if jwtData.Id <= 0 {
		service.ReturnJson(c, 401, errors.New("请先登录").Error(), gin.H{})
		c.Abort()
		return
	}

	c.Next()
}

func main() {
	r := gin.Default()

	config := cors.DefaultConfig()
	config.AllowOrigins = []string{"*"}
	r.Use(cors.New(config))

	//静态资源
	r.GET("/down", func(c *gin.Context) {
		fileName := c.DefaultQuery("filename", "")
		if fileName == "" {
			c.String(200, "获取文件名称不能为空")
			return
		}
		filePath := "./" + fileName
		c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
		c.Writer.Header().Add("Content-Type", "application/octet-stream")
		//c.String(200, filePath)
		c.File(filePath)
	})

	//程序请求
	r.GET("/ping", func(context *gin.Context) {
		context.String(200, "ping")
	})

	r.POST("/cgi-bin/auth/getuserdetail", service.Wx{}.AuthGetUserDetail)

	//后台登录
	admin := r.Group("admin")
	{
		admin.POST("login", admin2.Login{}.Login)                                //登录
		admin.POST("login-out", AdminJwtAuthentication, admin2.Login{}.LoginOut) //退出登录

		admin.POST("me", AdminJwtAuthentication, admin2.Login{}.Me)

		admin.POST("administrators/list", AdminJwtAuthentication, admin2.Administrators{}.List)
		admin.POST("administrators/insert", AdminJwtAuthentication, admin2.Administrators{}.Insert)
		admin.POST("administrators/save", AdminJwtAuthentication, admin2.Administrators{}.Save)
		admin.POST("administrators/save-passwd", AdminJwtAuthentication, admin2.Administrators{}.SavePasswd)
		admin.POST("administrators/status", AdminJwtAuthentication, admin2.Administrators{}.Del)

		admin.POST("clockin/repair-add", admin2.Clockin{}.RepairAdd)   //补卡新增
		admin.POST("clockin/repair-list", admin2.Clockin{}.RepairList) //补卡列表

		admin.POST("clockin/statistics-list", admin2.Clockin{}.StatisticsList)        //考勤概括
		admin.POST("clockin/statistics-list-new", admin2.Clockin{}.StatisticsListNew) //考勤概括
		admin.POST("clockin/detailed-list", admin2.Clockin{}.DetailedList)            //考勤明细
		admin.POST("clockin/detailed-staff", admin2.Clockin{}.DetailedStaff)          //考勤明细

		admin.POST("task/list", admin2.Task{}.TaskList)                          //考勤概括通知
		admin.GET("task/run", admin2.Task{}.TaskRun)                             //考勤概括通知
		admin.POST("task/statistics-notice", admin2.Task{}.TaskStatisticsNotice) //考勤概括通知
		admin.POST("task/statistics-export", admin2.Task{}.TaskStatisticsExport) //考勤概括导出
		//admin.POST("task/detailed-notice", admin2.Task{}.TaskDetailedNotice)     //考勤明细通知
		admin.POST("task/detailed-export", admin2.Task{}.TaskDetailedExport) //考勤明细导出

		//admin.POST("task/test-statistics-export", admin2.Task{}.TaskTestStatisticsExport)
		//admin.POST("task/test-detailed-export", admin2.Task{}.TaskSTesttatisticsExport)

		admin.POST("staff-ext", admin2.Staff{}.StaffExt)                                     //员工列表
		admin.POST("staff", AdminJwtAuthentication, admin2.Staff{}.List)                     //员工列表
		admin.POST("sync-staff", admin2.Staff{}.SyncStaff)                                   //同步员工列表
		admin.POST("department", AdminJwtAuthentication, admin2.Staff{}.Department)          //部门列表
		admin.POST("sync-department", AdminJwtAuthentication, admin2.Staff{}.SyncDepartment) //同步部门列表
		admin.POST("rule", admin2.Rule{}.Rule)                                               //规则获取
		admin.POST("sync-rule", admin2.Rule{}.SyncRule)                                      //规则同步

		//admin.POST("wx-work/checkin/getcheckin_monthdata", AdminJwtAuthentication, admin2.Wxwork{}.CheckinGetcheckinMonthdata)  //
		//admin.POST("wx-work/checkin/getcheckin_daydata", AdminJwtAuthentication, admin2.Wxwork{}.CheckinGetcheckinDaydata)      //
		//admin.POST("wx-work/checkin/getcheckindata", AdminJwtAuthentication, admin2.Wxwork{}.CheckinGetcheckindata)             //
		//admin.POST("wx-work/checkin/getcheckinschedulist", AdminJwtAuthentication, admin2.Wxwork{}.CheckinGetcheckinschedulist) //
		//admin.POST("wx-work/checkin/getcheckinoption", AdminJwtAuthentication, admin2.Wxwork{}.CheckinGetcheckinoption)         //

	}

	//编译linux运行程序
	//go env -w GOOS=linux
	//go env -w GOARCH=amd64
	//编译win运行程序
	//go env -w GOOS=windows
	//go env -w GOARCH=amd64
	//编译到指定目录
	//go build -o ./run/
	_ = r.Run(":8080")
}
